import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:furaha_mkopo/commonTool/fm_language_tool.dart';
import 'package:furaha_mkopo/manager/fm_global_manager.dart';
import '../commonTool/fm_color_tool.dart';

class FMToast {

  static OverlayEntry? _overlayEntry;

  static Timer? _timer;

  static void showText(String message) {
    message = FMLanguageTool.getText(message);
    _cancelToast();
    _overlayEntry = OverlayEntry(
        builder: (BuildContext context) {
          return _containerWidget(message: message);
    });

    _startTimer(const Duration(seconds: 2));
    Overlay.of(FMGlobalManager().context!).insert(_overlayEntry!);
  }

  static Widget _containerWidget({required String message}) {
    return Column(
      mainAxisSize: MainAxisSize.max,
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        Flexible(
          child: Padding(
            padding:const EdgeInsets.symmetric(horizontal: 40, vertical: 80),
            child: Builder(builder: (context){
              return Container(
                decoration: BoxDecoration(
                  color:FMColorTool.color('#1E232B',alpha: 0.9),
                  borderRadius: BorderRadius.circular(4),
                ),
                // 设置Toast背景色

                child: Padding(
                  padding:const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
                  child:Builder(builder: (context) {
                    return Text(
                      message,
                      textAlign:TextAlign.center,
                      style: const TextStyle(
                        decoration: TextDecoration.none,
                        fontSize: 14,
                        fontWeight: FontWeight.normal,
                        // 文本颜色
                        color: Colors.white,
                      ),
                    );
                  }),
                ),
              );
            }),
          ),
        ),
      ],
    );
  }

  static _cancelToast() async {
    _cancelTimer();
    _overlayEntry?.remove();
    _overlayEntry = null;
  }

  static void _startTimer(Duration? duration) {
    if (duration == null) return;
    _timer = Timer(duration, () {
      _cancelToast();
    });
  }

  static _cancelTimer() async {
    _timer?.cancel();
    _timer = null;
  }
}